// pages/search/index.js
import {goodsSearch} from '../../request/goods'
Page({
  data: {
    inputValue: '',
    isFocus: false,
    goods: []
  },
  TimeId: -1,
  async qsearch(query) {
    let data = {query}
    const goods = await goodsSearch(data)
    this.setData({
      goods
    })
  },

  /* 输入 */
  handleInput(e) {
    const {value} = e.detail
    // 校验合法
    if (!value.trim()) {
      this.setData({
        goods: [],
        isFocus: false,
      })
      return
    }
    this.setData({
      isFocus: true
    })
    clearTimeout(this.TimeId);
    this.TimeId = setTimeout(() => {
      this.qsearch(value);
    }, 1000);
  },

  /* 取消 */
  handleCancel() {
    this.setData({
      inputValue: '',
      isFocus: false,
      goods: []
    })
  },
})